package com.wangzg.module.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangzg.common.util.ResultUtil;
import com.wangzg.module.admin.mapper.KuaiDiMapper;
import com.wangzg.module.admin.model.kuaidi.KuaiDi;
import com.wangzg.module.admin.service.KuaiDiService;
import com.wangzg.module.admin.vo.base.ResponseVo;
import com.wangzg.common.util.ExcelUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Service
public class KuaiDiServiceImpl extends ServiceImpl<KuaiDiMapper, KuaiDi> implements KuaiDiService {

//    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private KuaiDiMapper kuaiDiMapper;


    @Override
    public List<KuaiDi> findByCondition(IPage<KuaiDi> page, KuaiDi kuaiDi) {
        return kuaiDiMapper.findByCondition(page, kuaiDi);
    }

    @Override
    public List<KuaiDi> getList(String username, String phone, String kuaidiNo) {
        List<KuaiDi> resultList = this.list(new LambdaQueryWrapper<KuaiDi>()
                .like(StringUtils.isNotBlank(username), KuaiDi::getUserName, username)
                .eq(StringUtils.isNotBlank(phone), KuaiDi::getPhone, phone)
                .eq(StringUtils.isNotBlank(kuaidiNo), KuaiDi::getKuaidiNo, kuaidiNo)
        );

        return resultList;
    }

    @Override
    public ResponseVo add(String username, String phone, String kuaiDiNo, String company) {

        KuaiDi kuaiDi = buildKuaiDiObject(username, phone, kuaiDiNo, company);
        int rowCount = this.baseMapper.insert(kuaiDi);
        if (rowCount == 0) {
            return ResultUtil.error("新增失败");
        } else {
            return ResultUtil.error("新增成功");
        }
    }

    /**
     * 构建KuaiDi对象
     * @param username
     * @param phone
     * @param kuaiDiNo
     * @param company
     * @return
     */
    private KuaiDi buildKuaiDiObject(String username, String phone, String kuaiDiNo, String company){
        username = StringUtils.defaultString(username);
        phone = StringUtils.defaultString(phone);
        kuaiDiNo = StringUtils.defaultString(kuaiDiNo);
        company = StringUtils.defaultString(company);
        KuaiDi kuaiDi = KuaiDi.builder()
                .userName(username.trim()).phone(phone.trim())
                .kuaidiNo(kuaiDiNo).company(company).createTime(new Date()).build();

        return kuaiDi;

    }

    @Override
    public ResponseVo removeById(Integer id) {

        int rowCount = kuaiDiMapper.deleteById(id);
        if (rowCount == 0) {
           return ResultUtil.error("删除失败");
        }else {
            return ResultUtil.error("删除成功");
        }
    }

    @Override
    public void importData(MultipartFile multipartFile) throws Exception {
        List<KuaiDi> kuaiDiList = ExcelUtils.getKuaiDiList(multipartFile.getInputStream());
        if(CollectionUtil.isNotEmpty(kuaiDiList)){
            //批量插入
            this.saveBatch(kuaiDiList);
        }
    }

    @Override
    public KuaiDi selectById(String id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void modify(KuaiDi kuaiDi) {
        baseMapper.updateById(kuaiDi);
    }

    @Override
    public void exportData(List<String> ids) {
        if(CollectionUtil.isNotEmpty(ids)){

        }
    }


}
